package com.zeng.sys.authmd.servicemodule.convert;

import com.zeng.nicecore.util.BeanUtil;
import com.zeng.sys.authmd.apimodule.vo.MenuVo;
import com.zeng.sys.authmd.apimodule.vo.PermissionVo;
import com.zeng.sys.authmd.servicemodule.entity.Permission;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
@Component
public class PermissionVoConvert {

    public List<PermissionVo> toPermissionVoList(List<Permission> permissions) {
        List<PermissionVo> permissionVos = new ArrayList<>(permissions.size());
        for (Permission permission : permissions) {
            permissionVos.add(BeanUtil.beanToVo(permission, new PermissionVo()));
        }
        return permissionVos;
    }

    public PermissionVo toPermissionVo(Permission permission) {
        return BeanUtil.beanToVo(permission, new PermissionVo());
    }

    public List<MenuVo> toMenuVoList(List<PermissionVo> permissionVos) {

        if (permissionVos == null) {
            return Collections.emptyList();
        }
        List<MenuVo> menuVos = new ArrayList<>(permissionVos.size());
        for (PermissionVo permissionVo : permissionVos) {
            MenuVo menuVo = new MenuVo();
            menuVo
                    .setId(permissionVo.getCode())
                    .setUrl(permissionVo.getUrl())
                    .setConfig(permissionVo.getPerConfig())
                    .setLabel(permissionVo.getName())
                    .setAvailable(permissionVo.getAvailable())
                    .setParentCode(permissionVo.getParentCode());
            if (!CollectionUtils.isEmpty(permissionVo.getChildrens())) {
                menuVo.setChildren(this.toMenuVoList(permissionVo.getChildrens()));
            }
            menuVos.add(menuVo);
        }
        return menuVos;
    }

}
